package com.yunlankeji.stemcells.activity.mine;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.usage.NetworkStatsManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.yunlankeji.ganxibaozhijia.R;
import com.yunlankeji.stemcells.model.request.UserInfo;
import com.yunlankeji.stemcells.network.HttpRequestUtil;
import com.yunlankeji.stemcells.network.NetWorkManager;
import com.yunlankeji.stemcells.network.callback.HttpRequestCallback;
import com.yunlankeji.stemcells.network.responsebean.ResponseBean;
import com.yunlankeji.stemcells.utils.LogUtil;
import com.yunlankeji.stemcells.utils.ToastUtil;
import com.yunlankeji.stemcells.utils.Utils;

import retrofit2.Call;

import static android.widget.Toast.LENGTH_SHORT;
import static com.blankj.utilcode.util.RegexUtils.isMobileExact;
import static com.blankj.utilcode.util.RegexUtils.isMobileSimple;

public class RegisterActivity extends AppCompatActivity {

    private static final String TAG = "RegisterActivity";
    TextView phoneTextView,messageTextView,passwordTextView,password2TextView,sureTextView,sendmessageTextView;
    private CountDownTimer CountDownTimer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        phoneTextView=(TextView)findViewById(R.id.register_phone_et);
        messageTextView=(TextView)findViewById(R.id.register_message_et);
        passwordTextView=(TextView)findViewById(R.id.register_password_et);
        password2TextView=(TextView)findViewById(R.id.register_password_et_2);
        sureTextView=(TextView)findViewById(R.id.register_sure_tv);
        sendmessageTextView=(TextView)findViewById(R.id.register_message_tv);
        //验证码发送
        sendmessageTextView.setOnClickListener(new View.OnClickListener() {
            private int type = 1;
            @Override
            public void onClick(View v) {
                String registerphone=phoneTextView.getText().toString().trim();
                String registerpwd=passwordTextView.getText().toString();
                if(TextUtils.isEmpty(registerphone)){
                    ToastUtil.showShort("请输入手机号");
                }else  if(!Utils.isMobile(registerphone)){
                    ToastUtil.showShort("请输入正确的手机号!");

                }
                else if (type == 1){
                    CountDownTimer = new CountDownTimer(60*1000,1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            sendmessageTextView.setText(millisUntilFinished/1000+"秒");
                            sendmessageTextView.setEnabled(false);

                        }

                        @Override
                        public void onFinish() {
                            sendmessageTextView.setText("重新获取");
                            sendmessageTextView.setEnabled(true);
                        }
                    };
                    //发起接口调用
                    UserInfo userInfo=new UserInfo();
                    userInfo.getVerificationCode();
                    userInfo.setMemberName("yunlankeji");
                    userInfo.setPhone(registerphone);
                    userInfo.setVerificationType("0");

                    Call<ResponseBean> call = NetWorkManager.getInstance().getRequest().requestsendCode(userInfo);
                    HttpRequestUtil.httpRequest(call, new HttpRequestCallback() {
                        @Override
                        public void onSuccess(ResponseBean response) {
                            LogUtil.d(TAG,"请求成功");
                            String data = (String) response.data;
                            ToastUtil.showShort("验证码发送成功,请注意查收!");
                            CountDownTimer.start();
                        }

                        @Override
                        public void onFailure(String msg) {
                            ToastUtil.showShortForNet(msg);
                            LogUtil.d(TAG, "请求失败");

                        }

                        @Override

                        public void onDefeat(String code, String msg) {
                            ToastUtil.showShortForNet(msg);
                            LogUtil.d(TAG, "请求失败");

                        }
                    });

                }

            }
        });
        //注册
        sureTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String registerphone=phoneTextView.getText().toString();
                String registerpwd=passwordTextView.getText().toString();
                String message = messageTextView.getText().toString().trim() + "";
                String registerpwd2=password2TextView.getText().toString();
                if (TextUtils.isEmpty(registerphone)){
                    ToastUtil.showShort("请输入手机号");
                }else if (!Utils.isMobile(registerphone)){
                    ToastUtil.showShort("请输入正确的手机号!");
                } else if (TextUtils.isEmpty(message)){
                    ToastUtil.showShort("验证码不能为空!");
                }else if(TextUtils.isEmpty(registerpwd)){
                    ToastUtil.showShort("请输入密码!");
                }else if(registerpwd.length()<6){
                    ToastUtil.showShort("密码不得小于6位");
                }else if(!registerpwd.equals(registerpwd2)){
                    ToastUtil.showShort("两次密码不一致,请重新输入!");
                }else{
                    UserInfo userInfo=new UserInfo();
                    userInfo.setPhone(registerphone);
                    userInfo.setPassword(registerpwd);
                    userInfo.setMemberName("yunlankeji");
                    userInfo.setCode(message);

                    Call<ResponseBean> call = NetWorkManager.getInstance().getRequest().requestRegister(userInfo);
                    HttpRequestUtil.httpRequest(call, new HttpRequestCallback() {
                        @Override
                        public void onSuccess(ResponseBean response) {
                            LogUtil.d(TAG ,"注册成功");
                            LogUtil.d(TAG, JSON.toJSONString(response.data));
                            ToastUtil.showShort("注册成功");
                        }

                        @Override
                        public void onFailure(String msg) {
                            ToastUtil.showShortForNet(msg);
                            LogUtil.d(TAG, "请求失败");
                        }

                        @Override
                        public void onDefeat(String code, String msg) {
                            ToastUtil.showShortForNet(msg);
                            LogUtil.d(TAG, "请求失败");
                        }
                    });
                }
            }
        });

    }
}